
// 刷新验证码
$('#refresh').click(function(){
	
	refreshCaptcha();
});

function refreshCaptcha(){
	
	$('#refresh img').attr('src','captcha.jpg?' + new Date());
}

// 验证码输入框回车
$("#captcha").keydown(function(event){
	
	if(event.keyCode ==13){
		adminLogin();
	}
})

/**
 * 登录
 * @returns
 */
function adminLogin(){
	
	// 开启按钮loadding
	var l = Ladda.create(document.querySelector('#login'));
	l.start();
	
	var url = "login/login";
	var username = $('#username').val();
	var password = $('#password').val();
	
	// 表单校验
	if (username == null || username.length == 0){
		layer.tips("用户名不能为空","#username", {
			  tipsMore: true
		});
		l.stop();
		return ;
	}
	if (password == null || password.length == 0){
		layer.tips("密码不能为空","#password", {
			  tipsMore: true
		});
		l.stop();
		return ;
	}
	/*if (captcha == null || captcha.length == 0){
		layer.tips("验证码不能为空","#captcha", {
			  tipsMore: true
		});
		return ;
	}*/
	
	$.ajax({
		type: "POST",
	    url: url,
	    data: $('#loginForm').serialize(),
	    dataType: "json",
	    success: function(data){
			if(data.code == 0){//登录成功
                localStorage.setItem("token", data.token);
                localStorage.setItem("userName", data.userName);
                top.location.href ='index.html';
			}else{
				layer.msg(data.msg);
				refreshCaptcha();
			}
		}
	}).always(function() {
		l.stop(); 
	});
	
}